﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Webapiv3.Domain.Entity;
using Webapiv3.inetrface;

namespace webapiv3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IRespository<User> _userrespository;
        public UsersController(IRespository<User> userrespository)
        {
            _userrespository = userrespository;
        }
        // GET: api/Users
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<User> Get()
        {
            return _userrespository.Table.ToList();
            //return new string[] { "value1", "value2" };
        }

        // GET: api/Users/5
        /// <summary>
        /// 获取指定Id的用户
        /// </summary>
        /// <param name="id">获取指定Id</param>
        /// <returns></returns>
        [HttpGet("{id}", Name = "Get")]
        public User Get(int id)
        {
            return _userrespository.GetById(id);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="value"></param>
        // POST: api/Users
        [HttpPost]
        public void Post([FromBody] User value)
        {
            _userrespository.Insert(value);
        }
        /// <summary>
        /// 更新指定用户数据
        /// </summary>
        /// <param name="id">用户Iid</param>
        /// <param name="value"></param>
        // PUT: api/Users/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] User value)
        {
            var user = _userrespository.GetById(id);
            if(user != null)
            {
                user.UserName = value.UserName;
                user.PassWord = value.PassWord;
                user.UpdateTime = DateTime.Now;

                _userrespository.UpDtae(user);
            }
            
        }
        /// <summary>
        /// 删除指定Id的用户
        /// </summary>
        /// <param name="id"></param>
        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public void  Delete(int id)
        {
            _userrespository.Delete(id);
        }
    }
}
